package com.qujia.station.service.impl;


import com.qujia.common.core.domain.entity.SysUser;
import com.qujia.common.exception.BaseException;
import com.qujia.common.utils.SecurityUtils;
import com.qujia.station.domain.Commodity;
import com.qujia.station.domain.InventoryBatch;
import com.qujia.station.domain.InventoryDetails;
import com.qujia.station.enums.InventoryReviewStatusEnum;
import com.qujia.station.mapper.CommodityMapper;
import com.qujia.station.mapper.InventoryBatchMapper;
import com.qujia.station.service.InventoryBatchService;
import com.qujia.station.service.InventoryDetailsService;
import com.qujia.station.utils.CodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service("inventoryBatchService")
@Slf4j
public class InventoryBatchServiceImpl implements InventoryBatchService {

    @Autowired
    private InventoryBatchMapper inventoryBatchMapper;

    @Autowired
    private InventoryDetailsService inventoryDetailsService;

    @Autowired
    private CommodityMapper commodityMapper;


    @Override
    @Transactional
    public long saveInventoryBatch(InventoryBatch inventoryBatch) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(Objects.isNull(user)){
            throw new BaseException("账户不存在，请用户登录");
        }
        long i = 0;
        inventoryBatch.setOperatorId(user.getUserId());
        inventoryBatch.setOperator(user.getUserName());
        inventoryBatch.setReviewerId(user.getUserId());
        inventoryBatch.setReviewerName(user.getUserName());
        inventoryBatch.setReviewTime(new Date());//审批时间
        inventoryBatch.setReviewStatus(InventoryReviewStatusEnum.APPROVED.getCode());//默认已审批
        synchronized (this){
            String code = CodeUtil.createCode();
            inventoryBatch.setBatchNo(code);
            i = inventoryBatchMapper.insertSelective(inventoryBatch);
        }
        if(i <= 0){
            throw new BaseException("新增库存盘点单失败");
        }
        List<InventoryDetails> inventoryDetailsList = inventoryBatch.getInventoryDetailsList();
        if(CollectionUtils.isEmpty(inventoryDetailsList)){
            return i;
        }
        // 获取自增id
        long id = inventoryBatch.getId();
        for (InventoryDetails inventoryDetails : inventoryDetailsList) {
            inventoryDetails.setBatchId(id);
            inventoryDetails.setStationId(inventoryBatch.getStationId());
            inventoryDetails.setStationName(inventoryBatch.getStationName());
            inventoryDetails.setStationId(inventoryBatch.getStationId());
            inventoryDetails.setStationName(inventoryBatch.getStationName());
            inventoryDetails.setCreateUserId(user.getUserId());
            inventoryDetails.setCreateUserName(user.getUserName());
            createInventoryDetails(inventoryDetails);
            inventoryDetailsService.saveInventoryDetails(inventoryDetails);
        }
        return i;
    }

    //组装数据
    private void createInventoryDetails(InventoryDetails inventoryDetails){
        if(Objects.nonNull(inventoryDetails.getCommodityId())){
            Commodity commodity = commodityMapper.selectCommodityById(inventoryDetails.getCommodityId());
            inventoryDetails.setCommodityName(commodity.getCommodityName());
            inventoryDetails.setCommodityCode(commodity.getCommodityCode());
            inventoryDetails.setCategoryId(commodity.getCategoryId());
            inventoryDetails.setCategoryName(commodity.getCategoryName());
            inventoryDetails.setUnit(commodity.getUnit());
            inventoryDetails.setBarCode(commodity.getBarCode());
        }
    }

    @Override
    public List<InventoryBatch> selectInventoryBatchList(InventoryBatch inventoryBatch) {
        return inventoryBatchMapper.selectInventoryBatchList(inventoryBatch);
    }


}
